﻿#include "OperSEASubSystem.h"
#include "GUIDialog/GUISubSystemDialog/GUISEASubSysFlatPlateDialog.h"
#include "GUIDialog/GUISubSystemDialog/GUISEASubSysDoubleCurvedPlateDialog.h"
#include "GUIDialog/GUISubSystemDialog/GUISEASubSysSinglyCurvedPlateDialog.h"
#include "GUIDialog/GUISubSystemDialog/GUISEASubSysBeamDialog.h"
#include "GUIDialog/GUISubSystemDialog/GUISEASubSysCavityDialog.h"
#include "GUIDialog/GUISubSystemDialog/GUICreateSEABeamSelectPicklDialog.h"
#include "GUIDialog/GUISubSystemDialog/GUICreateSEAShellSelectPicklDialog.h"
#include "GUIDialog/GUISubSystemDialog/GUICreateSEACavitySelectPicklDialog.h"

#include "FITK_Component/FITKAcousticsModel/FITKAbstractSEASubSys.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsSEAModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKSEASubSysShellPlate.h"
#include "FITK_Component/FITKAcousticsModel/FITKSEASubSysShellCurved.h"
#include "FITK_Component/FITKAcousticsModel/FITKSEASubSysBeam.h"
#include "FITK_Component/FITKAcousticsModel/FITKSEASubSysCavity.h"
#include "FITK_Component/FITKAcousticsModel/FITKAbstractSEAFace.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKSpectraDampingLossFactor.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsSpectraManager.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsPhysicsProp.h"


#include "GUIWidget/ControlPanel.h"
#include "GUIWidget/TreeWidgetGroup.h"

#include "GUIFrame/MainWindow.h"
#include "GUIFrame/CentralWidget.h"
#include "PickDataProvider/PickedDataProvider.h"
#include "PickDataProvider/PickedData.h"
#include "OperatorsInterface/GraphEventOperator.h"
#include "updateBrowserTable.h"

#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKAppFramework/FITKMessage.h"

#include "FITK_Interface/FITKInterfaceGeometry/FITKGeoCommandList.h"
namespace ModelOper
{

    bool OperSEASubSystem::execGUI()
    {
        GUI::MainWindow * mainWindow = dynamic_cast<GUI::MainWindow *>(FITKAPP->getGlobalData()->getMainWindow());
        if (!mainWindow || !_emitter)return false;
        //获取动作名称
        QString actionName = _emitter->objectName();
        QDialog* dialog = nullptr;
        //创建平板操作
        if (actionName == "actionCreateFlatPlate")
        {
            this->setArgs("IsCreate", true);
            this->setArgs("SEASubSysType", (int)Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEAFlatPlate);
            dialog = new GUI::GUICreateSEAShellSelectPicklDialog(this, FITKAPP->getGlobalData()->getMainWindow());
            connect(dialog, &QDialog::accepted, this, &OperSEASubSystem::reviseSEAOper);
        }
        //创建单曲率板操作
        else if (actionName == "actionCreateSinglyCurvedShell")
        {
            this->setArgs("IsCreate", true);
            this->setArgs("SEASubSysType", (int)Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEASinglyCurvedShell);
            dialog = new GUI::GUICreateSEAShellSelectPicklDialog(this, FITKAPP->getGlobalData()->getMainWindow());
            connect(dialog, &QDialog::accepted, this, &OperSEASubSystem::reviseSEAOper);
        }
        //创建双曲率板操作
        else if (actionName == "actionCreateDoubleCurvedShell")
        {
            this->setArgs("IsCreate", true);
            this->setArgs("SEASubSysType", (int)Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEADoublyCurvedShell);
            dialog = new GUI::GUICreateSEAShellSelectPicklDialog(this, FITKAPP->getGlobalData()->getMainWindow());
            connect(dialog, &QDialog::accepted, this, &OperSEASubSystem::reviseSEAOper);
        }
        //创建梁操作
        else if (actionName == "actionCreateBeam")
        {
            this->setArgs("IsCreate", true);
            this->setArgs("SEASubSysType", (int)Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEABeam);
            dialog = new GUI::GUICreateSEABeamSelectPicklDialog(this, FITKAPP->getGlobalData()->getMainWindow());
            connect(dialog, &QDialog::accepted, this, &OperSEASubSystem::reviseSEAOper);
        }
        //创建声腔操作
        else if (actionName == "actionCreateCavity")
        {
            this->setArgs("IsCreate", true);
            this->setArgs("SEASubSysType", (int)Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEACavity);
            dialog = new GUI::GUICreateSEACavitySelectPicklDialog(this, FITKAPP->getGlobalData()->getMainWindow());
            connect(dialog, &QDialog::accepted, this, &OperSEASubSystem::reviseSEAOper);
        }
        //修改SEA子系统操作
        else if (actionName.contains("Edit"))
        {
            this->setArgs("IsCreate", false);
            this->reviseSEAOper();
        }
        //移除子系统
        else if (actionName.contains("Delete"))
        {
            this->deleteSEAOper();
        }
        //窗口显示
        if (dialog)
            dialog->show();

        return false;
    }

    bool OperSEASubSystem::execProfession()
    {
        EventOper::GraphEventOperator* graphOper = FITKOPERREPO->getOperatorT<EventOper::GraphEventOperator>("GraphPreprocess");
        if (graphOper == nullptr)return false;


        //渲染层显示数据
        int objID = 0;
        if (this->argValue("objID", objID))
        {
            graphOper->updateGraph(objID, true);
        }

        //QList<int>shellIDs;
        //this->argValue("shellIDs", shellIDs);
        //for (int i : shellIDs)//如果为空则不调用
        //{
        //    graphOper->updateGraph(i, true);//显示组成声腔SEA板
        //}



        ModelOper::updateBrowserTable();

        GUI::MainWindow* mw = GUI::MainWindow::GetMainWindowFromFramework();
        if (mw == nullptr || objID == 0) return true;
        GUI::CentralWidget* cw = mw->getCentralWidget();
        if (cw == nullptr) return true;
        GUI::ControlPanel*  cp = cw->getControlPanel();
        if (cp == nullptr)return true;
        GUI::TreeWidgetGroup* modelTree = dynamic_cast<GUI::TreeWidgetGroup*>(cp->getTreeWidget(GUI::TreeWidgetType::PageGroup));
        if (modelTree == nullptr)return true;
        modelTree->updateNodeName(objID);
        return true;
    }

    void OperSEASubSystem::reviseSEAOper()
    {
        int dataID = 0;
        if (!this->argValue("DataID", dataID))return;
        //获取离散数据
        Acoustics::FITKAcousticsDiscModel* discModel = FITKAPP->getGlobalData()->getMeshData<Acoustics::FITKAcousticsDiscModel>();
        if (!discModel)return;
        //获取SEA点数据
        Acoustics::FITKAcousticsSEAModel* seaModel = discModel->getSEAModel();
        if (!seaModel)return;
        Acoustics::FITKAcousticsSEASubSysManager* subSysMgr = seaModel->getSEASubSysManager();
        if (!subSysMgr) return;
        Acoustics::FITKAbstractSEASubSys* dataSEAData = subSysMgr->getDataByID(dataID);
        if (!dataSEAData)return;
        //获取类型
        Acoustics::FITKAbstractSEASubSys::SEASubSysType type = dataSEAData->getSEASubSysType();
        //创建修改平板的窗口对象
        QDialog* dialog = nullptr;
        if (type == Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEAFlatPlate)
        {
            this->setArgs("SEASubSysType", (int)Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEAFlatPlate);
            Acoustics::FITKSEASubSysPlateShell* flatPlate = dynamic_cast<Acoustics::FITKSEASubSysPlateShell*>(dataSEAData);
            if (!flatPlate)return;
            dialog = new GUI::GUISEASubSysFlatPlateDialog(flatPlate, this, FITKAPP->getGlobalData()->getMainWindow());
        }
        //创建修改单曲率板的窗口对象
        else if (type == Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEASinglyCurvedShell)
        {
            this->setArgs("SEASubSysType", (int)Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEASinglyCurvedShell);
            Acoustics::FITKSEASubSysSinglyCurvedShell* singlyCurvedPlate = dynamic_cast<Acoustics::FITKSEASubSysSinglyCurvedShell*>(dataSEAData);
            if (!singlyCurvedPlate)return;
            dialog = new GUI::GUISEASubSysSinglyCurvedPlateDialog(singlyCurvedPlate, this, FITKAPP->getGlobalData()->getMainWindow());
        }
        //创建修改双曲率板的窗口对象
        else if (type == Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEADoublyCurvedShell)
        {
            this->setArgs("SEASubSysType", (int)Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEADoublyCurvedShell);
            Acoustics::FITKSEASubSysDoublyCurvedShell* doublyCurvedPlate = dynamic_cast<Acoustics::FITKSEASubSysDoublyCurvedShell*>(dataSEAData);
            if (!doublyCurvedPlate)return;
            dialog = new GUI::GUISEASubSysDoubleCurvedPlateDialog(doublyCurvedPlate, this, FITKAPP->getGlobalData()->getMainWindow());
        }
        //创建修改梁的窗口对象
        else if (type == Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEABeam)
        {
            this->setArgs("SEASubSysType", (int)Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEABeam);
            Acoustics::FITKSEASubSysBeam* beam = dynamic_cast<Acoustics::FITKSEASubSysBeam*>(dataSEAData);
            if (!beam)return;
            dialog = new GUI::GUISEASubSysBeamDialog(beam, this, FITKAPP->getGlobalData()->getMainWindow());
        }
        //创建修改声腔的窗口对象
        else if (type == Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEACavity)
        {
            this->setArgs("SEASubSysType", (int)Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEACavity);
            Acoustics::FITKSEASubSysCavity* cavity = dynamic_cast<Acoustics::FITKSEASubSysCavity*>(dataSEAData);
            if (!cavity)return;
            dialog = new GUI::GUISEASubSysCavityDialog(cavity, this, FITKAPP->getGlobalData()->getMainWindow());
        }
        if (!dialog)return;
        connect(dialog, &QDialog::accepted, this, &OperSEASubSystem::execProfession);
        dialog->show();
    }

    void OperSEASubSystem::deleteSEAOper()
    {
        int dataID = 0;
        if (!this->argValue<int>("DataID", dataID))return;
        //获取离散数据
        Acoustics::FITKAcousticsDiscModel* discModel = FITKAPP->getGlobalData()->getMeshData<Acoustics::FITKAcousticsDiscModel>();
        if (!discModel)return;
        //获取SEA点数据
        Acoustics::FITKAcousticsSEAModel* seaModel = discModel->getSEAModel();
        if (!seaModel)return;
        Acoustics::FITKAcousticsSEASubSysManager* subSysMgr = seaModel->getSEASubSysManager();
        if (!subSysMgr) return;
        Acoustics::FITKAbstractSEASubSys* dataSEAData = subSysMgr->getDataByID(dataID);
        if (!dataSEAData)return;
        //移除数据
        subSysMgr->removeDataObj(dataSEAData);
        this->execProfession();
    }
}

